package com.supermap.dataservice.cloud.api.menu;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.api.ApiMenuService;
import com.supermap.dubbo.api.cloud.api.SysApiMenu;
import com.supermap.dubbo.api.cloud.api.SysApiMenuParams;
import com.supermap.dubbo.api.cloud.cust.role.TreeNodeList;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @desc
 * @date 2019年05月13 11:12
 * @email 1301584033@qq.com
 */
@Service
public class ApiMenuServiceImpl implements ApiMenuService {

    @Autowired
    ApiMenuMapper apiMenuMapper;

    @Override
    public JSONUtils getSensiList(String type, String word,Integer limit) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(apiMenuMapper.getSensiList(type,word,limit));
    }

    @Override
    public JSONUtils getSensiGroupList() {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(apiMenuMapper.getSensiGroupList());
    }

    @Override
    public JSONUtils putSensi(String type, String word) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        apiMenuMapper.putSensi(type,word);
        return new JSONUtils();
    }

    @Override
    public JSONUtils delSensi(Integer id) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        apiMenuMapper.delSensi(id);
        return new JSONUtils();
    }

    @Override
    public JSONUtils getApiMenuList(String apiUserName) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        TreeNodeList treeNodeList=new TreeNodeList();
        treeNodeList.setName("全部节点");
        treeNodeList.setId(0);
        treeNodeList.setpId(0);
        treeNodeList.setOpen(true);
        List<TreeNodeList> sysAreaMenus= apiMenuMapper.getApiMenuList(0,apiUserName);
        for(TreeNodeList nodeList:sysAreaMenus){
            nodeList.setChildren(getAreaMenuList(nodeList,apiUserName));
            nodeList.setOpen(true);
        }
        treeNodeList.setChildren(sysAreaMenus);
        return new JSONUtils().setData(treeNodeList);
    }



    public List<TreeNodeList> getAreaMenuList(TreeNodeList sysMenu,String apiUserName){
        List<TreeNodeList> sysMenus = new ArrayList<>();
        if(sysMenu.getCategory()!=1){
            return  sysMenus;
        }
        List<TreeNodeList>  sysMenuList=apiMenuMapper.getApiMenuList(sysMenu.getId(), apiUserName);
        for(TreeNodeList sysMenu1:sysMenuList) {
            if (sysMenu1 != null) {
                List<TreeNodeList> result =getAreaMenuList(sysMenu1,apiUserName);
                if(sysMenu1.getCategory()==1){
                    sysMenu1.setChildren(result);
                }else{
                    sysMenu1.setChildren(result.size()>0?result:null);
                }
                sysMenu1.setOpen(true);
                sysMenus.add(sysMenu1);
            } else {
                continue;
            }
        }
        return sysMenus;
    }



    @Override
    public JSONUtils getApiMenuById(Integer menuId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        Map<String,Object> map=apiMenuMapper.getApiMenuById(menuId);
        map.put("params",apiMenuMapper.getMenuByIdParam(menuId));
        return new JSONUtils().setData(map);
    }

    @Override
    public JSONUtils putApiMenu(SysApiMenu sysApiMenu) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(apiMenuMapper.putApiMenu(sysApiMenu));
    }

    @Override
    public JSONUtils putApiMenuParms(SysApiMenuParams sysApiMenuParams) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(apiMenuMapper.putApiMenuParms(sysApiMenuParams));
    }

    @Override
    public JSONUtils saveApiMenuParms(SysApiMenuParams sysApiMenuParams) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(apiMenuMapper.saveApiMenuParms(sysApiMenuParams));
    }

    @Override
    public JSONUtils delApiMenuParms(Integer id) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(apiMenuMapper.delApiMenuParms(id));
    }

    @Override
    public JSONUtils delApiMenu(Integer menuId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(apiMenuMapper.delApiMenu(menuId));
    }

    @Override
    public JSONUtils saveApiMenu(SysApiMenu sysApiMenu) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        return new JSONUtils().setData(apiMenuMapper.saveApiMenu(sysApiMenu));
    }
}
